<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:sa="http://www.w3.org/1999/xhtml" lang="">
<head>
    <meta charset="utf-8">
    <title>角色管理</title>
    <link th:href="@{/pear/component/pear/css/pear.css}" rel="stylesheet"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" method="POST" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">角色名</label>
                    <div class="layui-input-inline">
                        <input id="roleName" type="text" name="roleName" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">角色代码</label>
                    <div class="layui-input-inline">
                        <input id="roleCode" type="text" name="roleCode" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="layui-btn layui-btn-md" lay-submit lay-filter="role-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="layui-btn layui-btn-md" id="resetBtn">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="role-table" lay-filter="role-table"></table>
    </div>
</div>
<script type="text/html" id="role-toolbar">
    <button class="layui-btn  layui-btn-sm" lay-event="add" sa:hasPermission="role_add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
</script>
<script type="text/html" id="role-bar">
    <button class="layui-btn  layui-btn-sm" sa:hasPermission="role_update" lay-event="edit"><i
            class="layui-icon layui-icon-edit"></i>
    </button>
    <button class="layui-btn layui-btn-danger layui-btn-sm" sa:hasPermission="role_delete" lay-event="remove"><i
            class="layui-icon layui-icon-delete"></i>
    </button>
    <button class="layui-btn layui-btn-warming layui-btn-sm" lay-event="page"><i class="layui-icon layui-icon-eye"></i>
    </button>
</script>
<script th:src="@{/pear/component/layui/layui.js}"></script>
<script th:src="@{/pear/component/pear/pear.js}"></script>
<script th:inline="javascript">
    layui.use(['table', 'form', 'jquery'], function () {
        let baseURL = /*[[@{/}]]*/ '';
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let cols = [
            [
                {type: 'checkbox'},
                {title: '角色名', field: 'roleName', align: 'left'},
                {title: '角色代码', field: 'roleCode', align: 'left'},
                {title: '描述', field: 'description', align: 'left'},
                {
                    title: '角色类型',
                    field: 'type',
                    align: 'center',
                    templet: function (d) {
                        // 根据角色类型字段的值，返回对应的文本描述
                        if (d.type === 0) {
                            return '部门角色';
                        } else if (d.type === 1) {
                            return '个人角色';
                        } else {
                            return '未知类型'; // 如果有其他未知的类型，可以在这里处理
                        }
                    }
                },
                {title: '操作', toolbar: '#role-bar', align: 'center'}
            ]
        ]

        table.render({
            elem: '#role-table',
            url: baseURL + 'role/list',
            type: 'get',
            request: {
                pageName: 'pageNum',
                limitName: 'pageSize'
            },
            page: true,
            cols: cols,
            skin: 'line',
            limit: 5,
            limits: [5, 10, 15, 20],
            toolbar: '#role-toolbar',
            parseData: function (res) {// 对从后端获得的数据进行规范，最后返回四个键值对
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total,
                    "data": res.data.list, //解析数据列表
                };
            },
            response: {
                statusName: "code",
                msgName: "msg",
                statusCode: 1000,
                dataName: "data"
            }
        });

        table.on('tool(role-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            } else if (obj.event === 'page') {
                window.page(obj);
            }
        });

        table.on('toolbar(role-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            }
        });
        form.on('submit(role-query)', function (data) {
            const roleName = data.field.roleName;
            let roleCode = data.field.roleCode;
            table.reload('role-table', {
                page: {
                    curr: 1
                },
                where: {
                    roleName: roleName,
                    roleCode: roleCode
                }
            })
            return false;
        });
        document.getElementById('resetBtn').addEventListener('click', function () {
            // 重新加载表格，不带任何过滤条件
            table.reload('role-table', {
                where: {}, // 清除过滤条件
                page: {curr: 1} // 重新从第一页开始
            });
        });
        form.on('switch(role-enable)', function (obj) {
            let roleId = obj.value;
            $.ajax({
                url: baseURL + "role/enable/" + roleId,
                type: 'post'
            })
        });

        window.add = function () {
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                move: true,
                area: ['800px', '720px'],
                content: 'role-add'
            });
        }
        let flag = false;
        window.edit = function (obj) {
            $.ajax({
                url: baseURL + 'role/getUsernameByRoleId',
                data: {
                    roleId: obj.data.roleId
                },
                type: "get",
                success: function (res) {
                    if (res.data.length) {
                        flag = true
                    }
                }
            })
            window.roleId = obj.data.roleId
            $.ajax({
                url: baseURL + "role/getPermissionIdsByRoleId",
                type: 'get',
                data: {
                    roleId: obj.data.roleId
                },
                success: function (res) {
                    window.list = res.data
                }
            })
            $.ajax({
                url: baseURL + "role/getChannelIdsByRoleId",
                type: 'get',
                data: {
                    roleId: obj.data.roleId
                },
                success: function (res) {
                    window.channelList = res.data
                }
            })
            $.ajax({
                url: baseURL + "role/getInfoIdsByRoleId",
                type: 'get',
                data: {
                    roleId: obj.data.roleId
                },
                success: function (res) {
                    window.infoList = res.data
                    layer.open({
                        type: 2,
                        title: '修改',
                        shade: 0.1,
                        area: ['800px', '720px'],
                        content: 'role-edit',
                        success: function (layero, index) {
                            let body = layer.getChildFrame('body', index);
                            let data = obj.data;
                            body.find('input[name=roleName]').val(data.roleName)
                            body.find('input[name=roleCode]').val(data.roleCode)
                            body.find('input[name=roleCode]').prop('disabled', flag)
                            body.find('input[name=description]').val(data.description)
                            let male = body.find("input[name=type][value=0]");
                            let female = body.find("input[name=type][value=1]");
                            male.prop('checked', data.type == 0 ? true : false);
                            female.prop('checked', data.type == 1 ? true : false);
                            form.render(male)
                            form.render(female)
                        }
                    });
                }
            })
        }
        window.remove = function (obj) {
            layer.confirm('确定要删除该角色', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: baseURL + 'role/getUsernameByRoleId',
                    data: {
                        roleId: obj.data.roleId
                    },
                    type: "get",
                    success: function (res) {
                        if (res.data.length > 0) {
                            layer.msg('该角色已分配，请先解除用户关系再删除', {
                                icon: 2,
                                time: 2000
                            });
                            layer.close(loading);
                        } else {
                            $.ajax({
                                url: baseURL + 'role/delete/' + obj.data['roleId'],
                                dataType: 'json',
                                type: 'post',
                                success: function (result) {
                                    layer.close(loading);
                                    table.reload('role-table', {where: {}, page: {curr: 1}});
                                    if (result.code === 1000) {
                                        layer.msg(result.msg, {
                                            icon: 1,
                                            time: 2000
                                        }, function () {

                                        });
                                    } else {
                                        layer.msg(result.msg, {
                                            icon: 2,
                                            time: 2000
                                        });
                                    }
                                }
                            })
                        }
                    }
                })
            });
        }
        window.page = function (obj) {
            // 打开弹层
            layer.open({
                type: 1,
                title: '角色对应用户',
                area: ['200px', '240px'], // 宽高
                content: '<div id="dataDialog" style="text-align: center; display: none;"></div>', // 初始时内容不显示
                success: function (layero, index) {
                    // 成功回调，layero是弹层DOM对象，index是弹层索引
                    $.ajax({
                        url: baseURL + 'role/getUsernameByRoleId', // 后端接口URL
                        type: 'GET',
                        data: {roleId: obj.data.roleId}, // 将roleId作为参数传递
                        success: function (res) {
                            if (res.code === 1000 && res.data && Array.isArray(res.data)) {
                                let Html = '<ul style="width: 100px; margin: 0 auto; padding: 10px;">';
                                res.data.forEach(function (data) {
                                    Html += '<li margin-top: 5px">' + data + '</li>'; // 将每个邮箱添加到列表中
                                });
                                Html += '</ul>';
                                $('#dataDialog').html(Html).show(); // 将邮箱列表填充到弹框内容区域，并显示
                            } else {
                                $('#dataDialog').html('无数据').show(); // 如果没有数据，显示“无数据”
                                layer.msg('获取数据失败'); // 失败提示
                            }
                        },
                        error: function () {
                            layer.msg('请求出错');
                            layer.close(index); // 关闭弹层
                        }
                    });
                }
            });
        };
        window.refresh = function () {
            table.reload('role-table');
        }
    })
</script>
</body>
</html>
